const formidable = require('formidable')
const http = require('http')
const util = require('util')

http.createServer((req,res)=>{
    console.log(req.url)
    if(req.url == '/upload' && req.method.toLowerCase() == 'post') {
        const form = new formidable.IncomingForm()
        form.parse(req,(err,fields,files)=>{
            res.writeHead(200,{'content-type':'text/plain'})
            res.write('received upload:\n\n')
            res.end(util.inspect({fields,files}))
        })
        return
    }

    // show a file upload form
    res.writeHead(200, {'content-type': 'text/html'})
    res.end(
        `
            <form action="/upload" enctype="multipart/form-data" method="post">
                <input type="text" name="title"><br>
                <input type="file" name="upload" multiple="multiple"><br>
                <input type="submit" value="Upload">
            </form>
        `
    )
}).listen(8888)